package cn.edu.zafu.bigdata.zengyadian;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileIOExample {

    // 将用户输入的文本写入文件的方法
    public static void writeToFile(String fileName, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 读取文件内容并打印到控制台的方法
    public static void readFromFile(String fileName) {
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = reader.readLine())!= null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        java.util.Scanner scanner = new java.util.Scanner(System.in);

        System.out.println("请输入要保存的文本内容：");
        String inputText = scanner.nextLine();

        // 假设学号是202202200204，可根据实际情况修改
        String fileName = "files/202202200204zyd.txt";

        writeToFile(fileName, inputText);
        System.out.println("文本已保存到文件，现在读取并显示文件内容：");
        readFromFile(fileName);

        scanner.close();
    }
}
